﻿using System.Web;
using System;

namespace SiteUnity.Frame.Common.Tools.Data
{
    public static class Session
    {
        public static void Update<T>(string key, T value)
        {

        }
        public static void Add<T>(string key, T value)
        {
            if (!Exists(key))
                HttpContext.Current.Session.Add(key, value);
            else
            {
                Remove(key);
                HttpContext.Current.Cache[key] = value;
            }
        }

        public static void Remove(string key)
        {
            if (Exists(key)) 
                HttpContext.Current.Session.Remove(key);
        }

        public static bool Exists(string key)
        {
            return HttpContext.Current.Session[key] != null;
        }

        public static T Get<T>(string key)
        {
            if (!Exists(key))
                HttpContext.Current.Session[key] = Activator.CreateInstance<T>();

            return (T)HttpContext.Current.Session[key];
        }

        public static void Dispose()
        {
            HttpContext.Current.Session.Clear();
            HttpContext.Current.Session.Abandon();
        }
    }
}
